/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry;

import io.sentry.util.Objects;

import java.util.UUID;

import org.jetbrains.annotations.NotNull;

/**
 * SpanId
 *
 * @since 2021-06-02
 **/
public final class SpanId {
    /**
     * EMPTY_ID
     */
    public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0).toString());

    private final @NotNull String value;

    /**
     * constructor
     *
     * @param value String
     */
    public SpanId(final @NotNull String value) {
        this.value = Objects.requireNonNull(value, "value is required");
    }

    /**
     * constructor
     */
    public SpanId() {
        this(UUID.randomUUID());
    }

    /**
     * constructor
     *
     * @param uuid uuid
     */
    private SpanId(final @NotNull UUID uuid) {
        this(uuid.toString().replace("-", "").substring(0, 16));
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        SpanId spanId = (SpanId) obj;
        return value.equals(spanId.value);
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }

    @Override
    public String toString() {
        return this.value;
    }
}
